package io.zhifei.modules.sys.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.DateUtils;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.modules.sys.system.dao.ExaminationDao;
import io.zhifei.modules.sys.system.entity.ExaminationEntity;
import io.zhifei.modules.sys.system.service.ExaminationServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 备考咨询
 *
 * @ClassNameExaminationImpl
 * @Description
 * @Author
 * @Date2020/11/16 15:18
 * @Version V1.0
 **/
@Service
public class ExaminationImpl extends ServiceImpl<ExaminationDao, ExaminationEntity> implements ExaminationServer {

    @Autowired
    private ExaminationDao examinationDao;

    @Override
    public PageUtils getlist(Integer page, Integer pageSize, Integer type) {
        // 根据type类型查询的时候 将page重新设置为第一页
        if (type != null && page != 1){
            page = 1;
        }
        Page pageParams = new Page(page, pageSize);
        Page<Map<String, Object>> pageResult = examinationDao.getList(pageParams, type);
        return new PageUtils(pageResult);
    }

    @Override
    public void add(ExaminationEntity examinationEntity, Long userId) {
        examinationEntity.setReleaseTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        examinationEntity.setCreateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        examinationEntity.setCreateUser(userId.toString());
        examinationDao.insert(examinationEntity);
    }

    @Override
    public void update(ExaminationEntity examinationEntity, Long userId) {
        examinationEntity.setUpdateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        examinationEntity.setUpdateUser(userId.toString());
        examinationDao.updateById(examinationEntity);
    }

    @Override
    public void delete(String[] ids) {
        for (String id : ids) {
            ExaminationEntity examinationEntity = new ExaminationEntity();
            examinationEntity.setId(Long.valueOf(id));
            examinationEntity.setFlag(1);
            examinationDao.updateById(examinationEntity);
        }
    }
}